# coding:utf-8

import os
import argparse
import sys
import traceback
import logging

import config
import backupdb

Parse = argparse.ArgumentParser()
Parse.add_argument('-t', nargs='?')
Args = Parse.parse_args()

logging.basicConfig(format="%(asctime)s [%(levelname)s] [%(filename)s] %(message)s",
                        filename=os.path.split(os.path.realpath(__file__))[0] + "/Log/Error.log")

Config = config.Config()
try:
    TK = Config.Show_Tk_Info(Args.t)
    if TK:
        DbLinkList = TK['dl'].split('-')
        for DbLink in DbLinkList:
            DbLink = Config.Show_Db_Link(DbLink)
            if DbLink:
                backupdb.BackupMyDb(Host=DbLink['host'],
                                      User=DbLink['user'],
                                      Password=DbLink['password'],
                                      DbName=DbLink['database'],
                                      Encoding=DbLink['encode'],
                                      Port=DbLink['port']).Backup()
    else:
        File = open(os.path.split(os.path.realpath(__file__))[0] + "/Log/Fail.log", 'a')
        print("不存在的计划任务：{0}".format(Args.t), file=File)
        File.close()
except Exception as E:
        E_Type, E_Value, E_TB = sys.exc_info()
        Trace_FileName, Trace_LineNo, Trace_FunName, Trace_Source = traceback.extract_tb(E_TB)[0]
        logging.error("[File:{0}] [LineNo:{1}] [Mes:{2}] [Source:{3}]".format(Trace_FileName, Trace_LineNo, E_Value, Trace_Source))